<?php
namespace app\user\controller;
use think\Session;
use think\Controller;
use think\Request;
use think\Db;
use Nette\Mail\Message;
use Nette\Mail\SendmailMailer;
use app\common\traits\controller\TraitController;
use app\common\controller\Api;
use app\user\traits\controllers\TraitUser;

/**
 * 
 */
class Cash extends Api
{
    use TraitUser;

    //前置操作
    protected $beforeActionList = [
        'checkLogin'
    ];
    /**
     * [checkLogin 判断是否登陆]
     * @return [type] [description]
     */
    protected function checkLogin()
    {
        if (!model('Users')->checkLogin()) {
            Session::set('redirect_url', Request::instance()->url(true));
            $this->redirect('@user/login');
            exit;
        }
    }
    
    public function getIndex()
    {   
        $usercapital = $this->getUsercapital();
        $blance = $usercapital['blance'];
        $this->assign('blance',$blance);

        $list = model('CapitalDetails')->getBlanceDetailLists($query = array(), $order = 'updated_at desc');
        $this->assign('list',$list);

        $incomelist = model('CapitalDetails')->getIncomeBlanceDetailLists($query = array(), $order = 'updated_at desc');
        $this->assign('incomelist',$incomelist);

        $spendlist = model('CapitalDetails')->getSpendBlanceDetailLists($query = array(), $order = 'updated_at desc');
        $this->assign('spendlist',$spendlist);

        return view('cash/index');
    }

    public function getCashForm()
    {   
        $usercapital = $this->getUsercapital();
        $blance = $usercapital['blance'];
        $this->assign('blance',$blance);

        $user_mobile = session('user.mobile');
		$user_id = session('user.id');
        // $user_data = model('Users')->where(['mobile'=>$user_mobile,'id'=>$user_id])->find();
		// if($user_data['authentication']!=1){
		// 	$this->error('您还没有通过实名认证');
		// 	return FALSE;
		// }

        $this->assign('real_name',$this->user->real_name);
        $bank_num_info = model('Cashing')->getWithdrawBankInfo();//halt($bank_num_info);
        return view('cash/cashform',['bank_num_info' => $bank_num_info]);
    }

    public function postCash()
    {
        
        $bank_num = input('post.bank_num');
        $open_bank = input('post.open_bank');
        if ($bank_num == '' || $open_bank  == '') {
            $this->error('请选择提现银行卡');
        }
        $amount = input('post.amount/d');

        $user_id = session('user.id');
        $count = $this->applyWithdraw($amount, $bank_num, $open_bank, $currency = 'CNY');
        if ($count) {
            $this->redirect('/cash/applyindex?bank_num='.$bank_num.'&amount='.$amount);
        }
        
    }
   
   /**
    * 添加开户行表单
    */
    public function getAddBank(){
        $user_mobile = session('user.mobile');
		$user_id = session('user.id');
        $user_data = model('Users')->where(['mobile'=>$user_mobile,'id'=>$user_id])->find();
		// if($user_data['authentication']!=1){
		// 	$this->error('您还没有通过实名认证');
		// 	return;
		// }
        return view('cash/add_bank',['real_name'=>$user_data['real_name']]);
    }
    /**
	 * 添加开户行
	 */
    public function postAddBank(Request $request){
        $bank_num = input('post.bank_num');
        $open_bank = input('post.open_bank');
        if ($bank_num == '' || $open_bank == '') {
            return $this->error('请填写开户行或银行卡号');
        }
    	if(Db::table('withdraw_card_info')->where('bank_num',$request->post('bank_num'))->find()){
    		$this->error('该银行卡已添加过');
    		return;
    	}
		if(empty($request->post('bank_num')) || empty($request->post('open_bank'))){
			$this->error('银行卡号或开户银行不能为空');
			return false;
		}
        $result = Db::table('withdraw_card_info')->insert($request->post(['user_id'=>session('user.id')]));
		if($result===1){
			$this->redirect('/cash/cashform');
		}
		$this->error('添加失败');
    }
    
    /**
	 * 提现申请提交页面
	 */
	public function getApplyIndex(){
		$getInfo = input('get.');
	    return view('cash/cashok',['getInfo'=>$getInfo]);
	}
    
	/**
	 * 删除开户行
	 */
	// public function postdel(Request $request){
	//     $result = Db::table('withdraw_card_info')->where('bank_num',$request->post('bank_num'))->delete();
	// 	if($result){
	// 		return json(['num'=>1,'meg'=>'删除成功']);
	// 	}
	// 	return json(['num'=>0,'meg'=>'删除失败']);
	// }
    
}

